#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXN 1000+1
char key[255], line[MAXN], code[130];
typedef struct LNode *Line;
struct LNode
{
    char ch;
    Line Next;
};
int Deduplication();
void Coding();

int main()
{
    FILE *IN, *OUT;
    if((IN = fopen("in.txt","r")) == NULL)
    {
        printf("The file do not exist.\n");
        return -1;
    }
    OUT = fopen("in_crpyt.txt","w");    
    gets(key);
    Deduplication();
    Coding();
    while(fgets(line, MAXN-1, IN) != NULL)
    {
        for(int i=0;line[i];i++)
        {
            if(line[i]>=32 && line[i]<=126)
                line[i] = code[line[i]];
        }
        fputs(line, OUT);
    }
    fclose(IN);
    fclose(OUT);
    return 0;
}

int Deduplication()
{
    int used[150], top = 0;
    memset(used, 0, sizeof(used));
    for(int i=0;key[i];i++)
    {
        if(!used[key[i]])
        {
            key[top++] = key[i];
            used[key[i]] = 1;            
        }
    }
    for(int i=32;i<=126;i++)
    {
        if(!used[i])
            key[top++] = (char)i;
    }
    return top;
}

void Coding()
{
    memset(code, 0, sizeof(code));
    Line L = (Line)malloc(sizeof(struct LNode));
    L->ch = key[0];
    L->Next = NULL;
    Line Rear = L;
    for(int i=1;i<95;i++)
    {
        Line Tmp = (Line)malloc(sizeof(struct LNode));
        Tmp->ch = key[i];
        Tmp->Next = NULL;
        Rear->Next = Tmp;
        Rear = Rear->Next;
    }
    Rear->Next = L;
    Line tmp = NULL;
    int ch, len = 95;
    while(len>1)
    { 
        ch = L->ch;
        tmp = L;
        Rear->Next = L->Next;
        L = Rear->Next;
        free(tmp); len--;
        for(int i=0;i<ch-1;i++)
        {
            L = L->Next;
            Rear = Rear->Next;
        }    
        code[ch] = L->ch;
    }
    if(len == 1)
        code[(L->ch)] = key[0];
}